/**
 * 登录模块，提供登录相关业务功能
 * 
 * @Date: 2015-6-16
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery", "msgServ","ajaxServ","datagridServ","sysmanMain","btnServ","bizProto"], 
	function($,msgServ,ajaxServ,datagridServ,sysmanMain,btnServ,bizProto) {

	'use strict';

	var    $typedg,$typegrid,$detaildg,$detailgrid,$edit_window,_defaultConfig,
			$panel,_bizProto,_moduleConfig,_buttonConfig,
			$edit_window_height=700,$edit_window_width=900,
			supplyBaseinfoEditUrl,
			supplyBasetypeQryUrl='/sysman/supplyBasetype/getTree.action',
			supplyBaseinfoQryUrl='/sysman/supplyBaseinfo/getTypeListDomain.action',
			supplyBaseinfoSaveUrl='/sysman/supplyBaseinfo/save.action',
			supplyBaseinfoDelUrl='/sysman/supplyBaseinfo/del.action'
		   	
    
	var	private_method = {
		init : function(_options) {
			if (window.console) {
				console.info('supplyBaseinfo modules gets ready and been loaded.');
			}
			_moduleConfig = sysmanMain.getCurrentModuConfig();
			_defaultConfig ={
				doc:null,
				rootId:null,				
				panelSelector:'div.well',
				subSystemCode:'sysman',
				moduleCode:'supplyBaseinfo',
				doccode:null
			}
					
			_defaultConfig = $.extend({},_defaultConfig,_options)			
			_bizProto = new bizProto(_defaultConfig);			
			_bizProto.gridHeight -= 15
			
			$edit_window = $('#win-doc-manage');
				
			_buttonConfig={
				"新增":private_method.add,
			    "编辑":private_method.edit,
				"删除":private_method.del
				 								
			}
			
			supplyBaseinfoEditUrl=_bizProto.rootPath+'/sysman/supplyBaseinfo/editPage.action'
			
			
			private_method.initElement(_bizProto.pageDoc)
		},
		
		//初始化路径
		initPath:function(){
			
		},
		initElement: function (doc) {		
			
			
			//初始化操作按钮
			var listTd = $(doc).find('div.doc-detail-list-btn')
			
			var _param = {
				bizcode:_moduleConfig.fullBizcode,
				rootPath:_bizProto.rootPath,
				container:listTd,
				callback:private_method.bindBtnEvent}
			btnServ.generateListBtn(_param);
			
			$typedg = $(doc).find("table.dg-type");
			
			
			var typeParam = {
				height : _bizProto.gridHeight,
				url : _bizProto.rootPath + supplyBasetypeQryUrl,
				onDblClickRow:private_method.getsupplyBaseinfo
			}
			if(_defaultConfig.doccode){
				typeParam.queryParams={doccode:_defaultConfig.doccode}
			}
			$typegrid=$($typedg).treegrid(typeParam);
			
			$detaildg = $(doc).find("table.dg-detail");
			
			$detailgrid=$($detaildg).datagrid({
				height :_bizProto.gridHeight,
				url : _bizProto.rootPath + supplyBaseinfoQryUrl,
				toolbar:$(listTd)
			});
			
		},
		
		searchFunction:function(value,name){
			var _param = {}
	    	_param['searchName'] = name;_param['searchValue'] = value
	    	return _param;
		},
		
		 getsupplyBaseinfo:function (rowData){
		 	$detailgrid.datagrid('load',{typeid:rowData.id})			 
		},
		//绑定按钮事件
		bindBtnEvent:function(){
			// 绑定按钮事件
			var btn = $(_bizProto.pageDoc).find('div.doc-detail-list-btn a.function-btn')
			$(btn).unbind('click');
			$(btn).bind('click', private_method.function_btn_listenner)			
			
			
		},
		// 功能按钮监听函数
		function_btn_listenner : function(e) {

			btnServ.applyButton({buttonEvt:e,buttonConfig:_buttonConfig})

		},
		// 新增函数
		add : function() {
			var selectedRecord = $($typegrid).treegrid('getSelected')					
			if (!selectedRecord) {					
				msgServ.Msgslide('请选择文档类型');
				return
			}
			$($detailgrid).datagrid('clearSelections')
			private_method.edit(false);
		},
		// 编辑,flag 为form按钮可见性标志
		edit : function(flag) {					
			var _param = {grid:$detailgrid,window:$edit_window,href:supplyBaseinfoEditUrl,width:$edit_window_width,height:$edit_window_height,flag:flag,existCheckElement:'form'}
			datagridServ.windowEdit(_param);					
		},
		
		
		// 删除
		del : function() {
			if (window.console) {
			console.info('supplyBaseinfo delete on air ...');
			}
			datagridServ.delRow({url:_bizProto.delUrl,target:$detailgrid});
		}
	}
	
	var public_method = {
		/**
		 * @param _options.doc	页面根节点
		 * @param _options.docroot	当前业务页面根节点
		 */
		init:function(_options){
			
			private_method.init(_options)
		}
	}	
	return public_method;
});